<%@page import="java.util.Enumeration"%>
<%@page import="com.cinemtl.beans.CartOrder"%>
<%@page import="java.util.HashMap"%>
<%@page import="com.cinemtl.beans.User"%>
<%@page import="com.cinemtl.action.UserAction"%>
<%@page import="com.cinemtl.action.MovieAction"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.cinemtl.beans.Movie"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	
	
<%
	if(session.getAttribute("user") != null){
		User user = (User) session.getAttribute("user");
		if(user.getUsername().equals("admin")){
	request.getRequestDispatcher("admin").forward(request,response);
		}
	}

	MovieAction.showAllMovies(request, response);
	ArrayList<Movie> movies = (ArrayList<Movie>) request
	.getAttribute("movies");
%>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cine-MTL | Movies</title>

<link href='http://fonts.googleapis.com/css?family=Open+Sans'
	rel='stylesheet' type='text/css'>
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="css/home.css" type="text/css" />
<link rel="stylesheet" href="css/flat-ui.min.css" type="text/css" />
</head>
<style>
img {
	border-radius: 10px;
}
</style>

<body>
	<div class="container">
		<div class="row">
<header>
				<a id="logo" href="home.jsp"><img src="images/cine-mtl-logo.png"></img></a>
				<div id="welcome"
					class="col-xs-offset-3 col-sm-offset-4 col-md-offset-6 col-lg-offset-7">
					<%
						if (session.getAttribute("user") != null) {
							User user = (User) session.getAttribute("user");
					%>

					<div id="showUsername">
						<span id="nameUser">Welcome <%=user.getUsername()%>
						</span><span class="fui-exit" id="logout"></span>
					</div>

					<%
						} else {
					%>
					<div id="showUsername">
						<span id="nameUser">Welcome Guest</span>
					</div>

					<%
						}
					%>
					<%
						Enumeration<String> keys=session.getAttributeNames();
						HashMap<Integer,CartOrder> orders = new HashMap<Integer,CartOrder>();
						while(keys.hasMoreElements())
						{
							if (keys.nextElement().equals("cart")) {
								orders = (HashMap<Integer, CartOrder>) session
										.getAttribute("cart");
							}
						}

						if (orders.isEmpty()) {
					%>
					<div id="cart">
						<span class="glyphicon glyphicon-shopping-cart" id="cartButton"><a
							href="cart.jsp" id="cartLink" data-qty="0">Cart(0)</a></span>
					</div>
					<%
						}else
							{
							
							
							int somme = 0;
							for(CartOrder order: orders.values())
							{
								somme+=order.getNbTicket();
							}
					%>
					<div id="cart">
						<span class="glyphicon glyphicon-shopping-cart" id="cartButton"><a
							href="cart.jsp" id="cartLink" data-qty="<%=somme%>">Cart(<%=somme%>)
						</a></span>
					</div>
					<%
						}
					%>
				</div>
				<nav class="navbar navbar-default navbar-embossed" role="navigation">
					<div class="navbar-header">
						<button type="button" class="navbar-toggle" data-toggle="collapse"
							data-target="#bs-example-navbar-collapse-1">
							<span class="sr-only">Toggle navigation</span> <span
								class="icon-bar"></span> <span class="icon-bar"></span> <span
								class="icon-bar"></span>
						</button>
					</div>
					<div class="collapse navbar-collapse"
						id="bs-example-navbar-collapse-1">
						<ul class="nav navbar-nav">
							<li><a href="home.jsp">Home</a></li>

							<li class="active"><a href="movies.jsp">Movies</a></li>
							<li><a href="contact.jsp">Contact</a></li>
						</ul>
						<form class="navbar-form navbar-right" action="search"
							method="get" role="search">
							<div class="form-group">
								<div class="input-group">
									<input class="form-control" id="navbarInput-01" type="search"
										name="search" placeholder="Search"> <span
										class="input-group-btn">
										<button type="submit" class="btn">
											<span class="fui-search"></span>
										</button>
									</span>
								</div>
							</div>
						</form>
						<%
							if (session.getAttribute("user") == null) {
						%>
						<div id="inout">
							<ul class="nav navbar-nav navbar-right">
								<li><a href="#" data-toggle="modal"
									data-target="#loginModal" class="">Log in</a></li>
								<li><a href="register.html">Sign up</a></li>
							</ul>
						</div>
						<%
							}
						%>
					</div>

				</nav>
			</header>
			
			<div class="row">
				<div class="text-center toolbar">
					<div class="btn-group" data-toggle="buttons">
					<%ArrayList<String> categories= new ArrayList<String>();
					for(Movie movie : movies) 
						if(!categories.contains(movie.getType())){
					categories.add(movie.getType());
						}
					%>
					<a class="btn btn-primary btn-categorie" data-categorie="all"><input type="radio">All</a>
					<%for(String categorie : categories){%>
						<a class="btn btn-primary btn-categorie" data-categorie="<%=categorie%>"><input type="radio"><%=categorie%></a>
						<%} %>
					</div>
				</div>



				<%
					for (Movie movie : movies) {
				%>

				<div class="col-xs-9 col-sm-5 col-md-4 col-lg-3 movieShowcase" data-type=<%=movie.getType()%>>
					<div class="thumbnail">
						<a href="movie.jsp?id=<%=movie.getId()%>"> <img
							src="<%=movie.getUrlImage()%>" alt="" width="75%">
						</a>
						<div class="caption text-center">
							<h4>
								<a href="#"><%=movie.getTitle()%></a>
							</h4>
						</div>
						<div class="row">
							<div
								class="col-xs-offset-2 col-sm-offset-2 col-md-offset-2 col-lg-offset-2">
								<a class=" btn btn-info" href="movie.jsp?id=<%=movie.getId()%>">Show
									Info</a> <a data-movie="<%=movie.getId()%>"
									class="btn btn-warning addCartMovies">Add Cart</a>
							</div>
						</div>


					</div>

				</div>

				<%
					}
				%>
			</div>

			<div class="modal fade" id="loginModal" tabindex="-1" role="dialog"
				aria-labelledby="myModalLabel" aria-hidden="true">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal"
								aria-hidden="true">&times;</button>
							<h4 class="modal-title">Log in</h4>
						</div>

						<div id="loginDiv" class="modal-body">
							<form id="loginForm" method="post" class="form-horizontal"
								action="">
								<div class="login-form">
									<div class="form-group">
										<input type="text" class="form-control login-field"
											name="username" placeholder="Enter your name" id="login-name"
											required /> <label class="login-field-icon fui-user"
											for="login-name"></label>
									</div>

									<div class="form-group">
										<input type="password" class="form-control login-field"
											name="password" placeholder="Password" id="login-pass"
											required /> <label class="login-field-icon fui-lock"
											for="login-pass"></label>
									</div>
									<input class="btn btn-primary btn-lg btn-block" type="button"
										id="submit" value="login"> <a class="login-link"
										href="redirect">Lost your password?</a>

								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<footer>
			<div class="row">
				<div class="col-lg-12">
					<h5>Copyright &copy; Cine-MTL 2015</h5>
				</div>
			</div>
		</footer>
	</div>
	<script
		src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
	<script src="js/flat-ui.min.js"></script>
	<script type="text/javascript" src="js/sha1.js"></script>
	<script src="js/main.js"></script>
	<script src="js/cart.js"></script>
	<script>
		$('.input-group').on('focus', '.form-control', function() {
			$(this).closest('.input-group, .form-group').addClass('focus');
		}).on('blur', '.form-control', function() {
			$(this).closest('.input-group, .form-group').removeClass('focus');
		});
	</script>
</body>

</html>